﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Shapes;
using Silvermoon.Controls.Base;
using Silvermoon.Utils;
using System.Drawing;
using Silvermoon.UI;
using Silvermoon.Windows;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Base Control for all controls that return a value from GetChildControls();
    /// Unless Control, CompositeControlBase has Controls set to an instance of <see cref="T:ControlCollection"/>.
    /// </summary>
    public abstract class CompositeControlBase : ChromeControl
    {
        #region fields

        private StyleDictionary styles;
        private ControlCollection controls;

        /// <summary>
        /// If overloaded, returns a ControlCollection used to store child controls.
        /// </summary>
        /// <returns>ControlCollection, otherwise null</returns>
        protected internal override ControlCollection GetVisibleControls()
        {
            if (controls == null) controls = new ControlCollection(this);
            return controls;
        }


        #endregion
        #region dtor
        protected override void Dispose(bool dispose)
        {
            base.Dispose(dispose);
            if (controls != null) controls.Clear();
            styles = null;
        }
        #endregion
        #region props

        /// <summary>
        /// Gets the style dictionary, otherwise null.
        /// </summary>
        public override StyleDictionary Styles
        {
            get
            {

                if (styles == null) styles = new StyleDictionary();
                return styles;
            }
            set { styles = value; }
        }

        public override bool HasStyles
        {
            get { return styles != null; }
        }


        #endregion
        #region methods

        /// <summary>
        /// Renders the control with all shapes and child controls to the specified renderer.
        /// Occurs  after transformations are applied.
        /// </summary>
        /// <param name="renderer">Renderer which will render the control.</param>
        protected internal override void OnRender(Silvermoon.Core.Renderer renderer)
        {
            var shapes = Shapes;
            int alpha = renderer.Alpha;
            if (shapes != null) renderer.RenderShapes(shapes);

            RenderChildren(renderer);
            Shape chrome = Chrome;
            if (chrome != null)
            {
                renderer.RenderShape(chrome, alpha);
            }
        }

        protected virtual void RenderChildren(Silvermoon.Core.Renderer renderer)
        {
            var controls = this.controls;
            if (controls != null && controls.First != null)
            {
                Rectangle scissorBox = renderer.ScissorBox;
                renderer.RenderControls(controls);
                renderer.ScissorBox = scissorBox;
            }
        }



        #endregion


    }
}
